package com.thrc.borrow.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.entity.Job;
import com.thrc.borrow.service.JobService;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.JobVo;

/**
 * 企业相关
 * @author suker.zhou
 *
 */
@Controller
@RequestMapping("/job")
public class JobController {

	@Autowired
	private JobService jobService;

	@RequestMapping("/list/page")
	public String goUserJsp() {

		return "sys/jobList";
	}

	@RequestMapping("/getById")
	public void selectById(HttpServletResponse response, Job t) {
		JobVo vo = jobService.selectById(t);
		ResponseUtil.print(response, vo);
	}

	@RequestMapping("/selectAll")
	public void selectAll(HttpServletResponse response, Job t) {
		try {
			List<JobVo> UserList = jobService.selectAll(t);
			ResponseUtil.print(response, UserList);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/search")
	public void search(HttpServletResponse response, Job t) {
		try {
			List<JobVo> userList = jobService.selectAll(t);
			if(userList.size()>0){
				ResponseUtil.print(response, ReturnCode.SUCCESS_CODE,"操作成功",userList);
			}else{
				ResponseUtil.print(response, ReturnCode.FAIL_CODE,"没有加载到数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/selectByPage")
	public void selectByPage(HttpServletResponse response, Job t) {

		JSONObject retJson = new JSONObject();
		try {
			int totleCount = jobService.selectCount(t);
			retJson.put("total", totleCount);
			if (totleCount == 0) {
				retJson.put("data", null);
				ResponseUtil.print(response, retJson);
				return;
			}
			List<JobVo> UserList = jobService.selectByPage(t);
			retJson.put("data", UserList);
			ResponseUtil.print(response, retJson);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	
	@RequestMapping("/insertJob")
	public void insert(HttpServletResponse response, 
					   HttpServletRequest request, 
					   Job t) {

		try {
			t.setCreateTime(new Date());
			jobService.insert(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/updateJob")
	public void update(HttpServletResponse response, Job t) {
		try {
			jobService.update(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/deleteJob")
	public void delete(HttpServletResponse response, Job t) {

		try {
			jobService.delete(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
}
